<template>
    <div class="table-box">
        <div class="table-search">
            <el-form :inline="true" :model="form">
                <el-form-item>
                    <el-input v-model="form.bill_id" placeholder="账单ID"/>
                </el-form-item>
                <el-form-item>
                    <el-input v-model="form.uid" placeholder="商户ID"/>
                </el-form-item>
                <el-form-item>
                    <el-input v-model="form.txid" placeholder="交易Hash"/>
                </el-form-item>
                <el-form-item>
                    <el-input v-model="form.address" placeholder="接收地址"/>
                </el-form-item>
                <el-form-item>
                    <el-select v-model="form.chain_name" placeholder="主链名称" style="width:120px;">
                        <el-option
                            v-for="item in chainsList"
                            :key="item.chain_id"
                            :label="item.chain_name"
                            :value="item.chain_name"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-select v-model="form.coin_id" placeholder="币种名称" style="width:120px;">
                        <el-option
                            v-for="item in coinsList"
                            :key="item.coin_id"
                            :label="item.coin_name"
                            :value="item.coin_id"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-select v-model="form.token_name" placeholder="Token名称" style="width:120px;">
                        <el-option
                            v-for="item in tokensList"
                            :key="item.token_id"
                            :label="item.token_name"
                            :value="item.token_name"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-select v-model="form.order_type" placeholder="订单类型" style="width:120px">
                        <el-option
                            v-for="item in orderTypeList"
                            :key="item.type"
                            :label="item.name"
                            :value="item.type"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-select v-model="form.status" placeholder="订单状态" style="width:120px;">
                        <el-option
                            v-for="item in statusList"
                            :key="item.type"
                            :label="item.name"
                            :value="item.type"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-date-picker
                        v-model="form.timeData"
                        type="datetimerange"
                        range-separator="至"
                        start-placeholder="开始日期"
                        end-placeholder="截止日期" :default-time="pickerDefaultTime"
                    />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onQuery">查询</el-button>
                    <el-button @click="onReset">重置</el-button>
                    <!--<el-button type="primary" plain @click="onExport">导出</el-button>-->
                </el-form-item>
            </el-form>
        </div>
        <div class="card content-box">
            <div class="table">
                <el-table :data="tableData" border v-loading="loading">
                    <el-table-column prop="id" label="ID" align="center"/>
                    <el-table-column prop="merchant_id" label="商户ID" align="center"/>
                    <el-table-column prop="coin_name" label="币种名称" align="center"/>
                    <el-table-column prop="chain_name" label="主链名称" align="center"/>
                    <el-table-column prop="token_name" label="Token名称" align="center"/>
                    <el-table-column label="订单类型" align="center">
                        <template #default="scope">
                          <el-tag :type="scope.row.order_type == 1 ? 'success' : 'warning'">
                              {{ scope.row.order_type == 1 ? "发送" : "接收" }}
                          </el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column prop="amount" label="交易数量" align="center"/>
                    <el-table-column label="费用" align="center">
                        <template #default="scope">
                            {{ scope.row.fee }} {{ scope.row.fee_unit }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="from_address" label="发送地址" align="center"/>
                    <el-table-column label="接收地址" align="center">
                        <template #default="scope">
                            {{ scope.row.to_address }} {{ scope.row.to_memo ? '(memo=' + scope.row.to_memo +')' : '' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="状态" align="center">
                        <template #default="scope">
                          <el-tag :type="scope.row.status == 1 ? 'success' : scope.row.status == 2 ? 'danger' : 'warning'">
                              {{ scope.row.status == 1 ? "成功" : scope.row.status == 2 ? "失败" : "处理中" }}
                          </el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column prop="createtime" label="创建时间" align="center" :formatter="formatterTime"/>
                    <el-table-column prop="confirm_time" label="完成时间" align="center" :formatter="formatterTime"/>
                    <el-table-column prop="bill_id" label="账单ID" align="center"/>
                    <el-table-column prop="txid" label="交易Hash" align="center"/>
                    <el-table-column prop="txid_n" label="交易下标" align="center"/>
                    <el-table-column prop="confirmations" label="确认数" align="center"/>
                    <el-table-column prop="txid_time" label="Hash时间" align="center" :formatter="formatterTime"/>
                    <el-table-column prop="open_id" label="三方托管ID" align="center"/>
                    <el-table-column label="三方托管费用" align="center">
                        <template #default="scope">
                            {{ scope.row.open_fee }} {{ scope.row.open_fee_unit }}
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" align="center" fixed="right" width="150">
                        <template #default="scope">
                            <el-button type="primary" plain
                            v-if="scope.row.status == 0 && scope.row.order_type == 1"
                            @click="handleOperate(scope.$index, scope.row, 'confirm')"
                            >确认</el-button>
                            <el-button type="primary" plain
                            v-if="scope.row.status == 0 && scope.row.order_type == 1"
                            @click="handleOperate(scope.$index, scope.row, 'cancel')"
                            >撤销</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="table-pagination">
                    <el-pagination
                        :current-page="pageable.pageNum"
                        :page-size="pageable.pageSize"
                        :page-sizes="[10, 25, 50, 100]"
                        :background="true"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="pageable.total"
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                    ></el-pagination>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import {ref, onMounted, reactive, computed} from "vue";
import {formatterTime, formatter, timestamp, pickerDefaultTime} from "@/utils/utils";
import {walletTransactionsApi} from "@/api/payServerse/wallet";
import {GlobalStore} from "@/store";

const globalStore = GlobalStore();
const loading = ref(false);
const coinsList = computed(() => globalStore.coins);
const chainsList = computed(() => globalStore.chains);
const tokensList = computed(() => globalStore.tokens);
const orderTypeList = [
    {
        type: 1,
        name: "发送",
    },
    {
        type: 2,
        name: "接收",
    },
];
const statusList = [
    {
        type: 0,
        name: "处理中",
    },
    {
        type: 1,
        name: "已完成",
    },
    {
        type: 2,
        name: "失败",
    },
];
const tableData = ref([]);

const formData = {
    timeData: [],
    coin_id: "",
    token_name: "",
    chain_name: "",
    merchant_id: "",
    bill_id: "",
    txid: "",
    address: "",
    order_type: "",
    status: "",
};
const form = reactive({...formData});

const pageable = reactive({
    pageNum: 1,
    pageSize: 10,
    total: 0,
});

// 参数
const param = () => {
    return {
        page: pageable.pageNum,
        pagesize: pageable.pageSize,
        coin_id: form.coin_id,
        token_name: form.token_name,
        chain_name: form.chain_name,
        merchant_id: form.merchant_id,
        bill_id: form.bill_id,
        txid: form.txid,
        address: form.address,
        order_type: form.order_type,
        status: form.status,
        start: form.timeData[0] ? timestamp(form.timeData[0].valueOf()) : "",
        end: form.timeData[1] ? timestamp(form.timeData[1].valueOf()) : "",
    };
};

// 获取表格数据
const getTbaleData = async () => {
    loading.value = true;

    try {
        const res = await walletTransactionsApi(param());
        loading.value = false;
        tableData.value = res.data.results;
        pageable.total = res.data.count;
    } catch (error) {
        loading.value = false;
    }
};
// 重置
const onReset = () => {
    Object.assign(form, formData);
};
// 查询
const onQuery = async () => {
    getTbaleData();
};
// 条数
const handleSizeChange = (number) => {
    pageable.pageSize = number;
    getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
    pageable.pageNum = number;
    getTbaleData();
};
onMounted(() => {
    getTbaleData();
});
</script>
